package gitdemo1.git3.list;

public class LNode {
    int val;
    LNode next;
    LNode() {}
    LNode(int val) { this.val = val; }
    LNode(int val, LNode next) { this.val = val; this.next = next; }

    public static void main(String[] args) {
//        LNode lNode = new LNode(1, lNode2);
//        LNode lNode = new LNode(1, lNode2);
        LNode lNode5 = new LNode(4, null);
        LNode lNode4 = new LNode(3, lNode5);
        LNode lNode3 = new LNode(2, lNode4);
        LNode lNode2 = new LNode(1, lNode3);
        LNode lNode1 = new LNode(0, lNode2);
        divide(lNode1);

    }

        public static void del_x (LNode link,int x){
        LNode p = link.next;
        LNode pre = link;
        while (p != null) {
            if (p.val == x) {
                pre.next = p.next;
                p = p.next;
            } else {
                pre = p;
                p = p.next;
            }

        }

    }

        public static void revers (LNode list){
        if (list == null) {
            return;
        }
        revers(list.next);
        System.out.println(list.val);
    }

        public static void del_m (LNode list){
        LNode head = list;
        LNode pre1 = list, p = list, pre2 = list;
        int min = p.val;
        list = list.next;
        while (list != null) {
            if (list.val < min) {
                pre1 = pre2;
                p = list;
            }
            pre2 = pre2.next;
            list = list.next;
        }
        pre1.next = p.next;
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }

        public static void revers2 (LNode list){
        LNode pre = null, p = list.next, r = p.next;

        while (r != null) {
            p.next = pre;
            pre = p;
            p = r;
            r = r.next;
        }

        p.next = pre;
        list.next = p;

        while (list != null) {
            System.out.println(list.val);
            list = list.next;
        }


    }

        public static void sort1 (LNode list){
        LNode pre, p = list.next, r = p.next, temp;
//        head.next=list.next;
        p.next = null;
        while (p != null) {
            temp = list.next;
            pre = list;
            while (p.val > temp.val) {
                pre = pre.next;
                temp = temp.next;
            }
            pre.next = p;
            p = p.next;

        }
        while (list != null) {
            System.out.println(list.val);
            list = list.next;
        }

    }

        public static void divide(LNode list){

        LNode listB = new LNode(-1, null);
        int k=1;

        LNode la=list,lb=listB,p=list.next;
        while(p!=null){
            if(k%2==1){
                la.next=p;
                la=p;
                p=p.next;
                k++;
            }else {
                lb.next=p;
                lb=p;
                p=p.next;
                k++;
            }

        }

        la.next=null;
        lb.next=null;

        while(list.next!=null){
            System.out.println(list.next.val);
            list=list.next;
        }
        System.out.println("------");;

       while(listB.next!=null){
           System.out.println(listB.next.val);
           listB=listB.next;
       }

    }

    public static void mergeList(LNode listA,LNode listB){

        LNode temp,r,la=listA.next,lb=listB;//他们继续他们的工作
        listA.next=null;//这样可以和前面的断开来，

        while(la!=null&&lb!=null){
            if(la.val<=lb.val){
                r=la.next;//保存后面的节点
                temp=listA.next;
                la.next=temp;
                listA.next=la;
                la=r;
            }else {
                r=lb.next;
                temp=listA.next;
                lb.next=temp;
                listA.next=lb;
                lb=r;
            }
        }
        //la还有剩余
        while(la!=null){
            r=la.next;//保存后面的节点
            temp=listA.next;
            la.next=temp;
            listA.next=la;
            la=r;
        }
        while(lb!=null){
            r=lb.next;
            temp=listA.next;
            lb.next=temp;
            listA.next=lb;
            lb=r;
        }


    }

    public static void isParent(LNode A,LNode B){
        LNode pa=A.next,pre=A.next,pb=B.next;
        while(pa!=null&&pb!=null){
            if(pb.val== pa.val){
                pa=pa.next;
                pb=pb.next;
            }else {
                pb=B.next;
                pa=pre.next;
                pre=pre.next;
            }
        }
        if(pb==null){

        }


    }

    public static void circle(LNode h1,LNode h2){
        LNode pa=h1.next,pb=h2.next;
        //pa是h1的尾部
        while(pa!=null&&pa.next.next!=h1.next){
            pa=pa.next;
        }
        //pb是h2的尾部
        while(pb!=null&&pb.next.next!=h2.next){
            pb=pb.next;
        }

        pa.next=h2.next;
        pb.next=h1;


    }

    public static void del_min(LNode h1){
        LNode pre,pa=h1;
        pre=pa.next;
        while (pa!=null){

            if(pa.next.val<pre.val){
                pre=pa;
            }
            pa=pa.next;
            if(pa.next==h1){
                System.out.println(pre.val);
                LNode temp= pre.next;
                pre.next=temp.next;

                pa=h1.next;
            }
        }
    }


}
